#include "../../inc/protocol.h"
#include "../../inc/my_sqlite3.h"
static int history(int sockfd);
static int serach_word(int sockfd);
static int login(int sockfd);
static int regist(int sockfd);
void display_mainui2();
void display_mainui1();
static int create_client_socket(const char *ip,unsigned short port);
static int client_main_loop(int sockfd);
int main(int argc,char *argv[])
{
	int sockfd = -1;
	int port = 0;
	
	if(argc < 3)
	{
		printf("The argument is too few\n");
		return 1;
	}

	sscanf(argv[2],"%d",&port);
	if(port < 0 || port > 0xFFFF)
	{
		printf("The port is invalid\n");
		return 2;
	}

	srand(time(NULL));

	sockfd = create_client_socket(argv[1],port);
	if(sockfd < 0)
	{
		return 3;
	}

	client_main_loop(sockfd);

	/*无需继续与服务器进行数据交互，应立即调用close关闭socket函数创建的引擎*/
	close(sockfd);
	sockfd = -1;

	return 0;
}
static int client_main_loop(int sockfd)
{
	int op1 = 0;
	int exitflag = 0;
	int flag = 0;
	int ret = 0;
	int op2 = 0;
	while(1)
	{
		/*1. 用户选择使用哪种服务*/
		display_mainui1();
		scanf("%d",&op1);
		/*2. switch用户的选择*/
		switch(op1)
		{
			case 1://使用注册服务
				ret = regist(sockfd);
				break;
			case 2://使用登录服务
				ret = login(sockfd);
				if(ret == 0 )
				{
				    display_mainui2();
				    while(1)
				    {
					scanf("%d",&op2);
					switch(op2)
					{
					    case 1://使用查词服务
						ret = serach_word(sockfd);
						display_mainui2();
						break;
					    case 2 ://使用查历史记录服务
						ret = history(sockfd);
						display_mainui2();
						break;
					    case 0 :
						flag = 1;
						break;
					}
					if(flag)
					{
					    break;    
					}
				    }
				}
				break;
			case 0 :
			    exitflag = 1;
			    break;	
		}
		if(exitflag)
		{
			break;
		}
	}
	return 0;
}

void display_mainui1()
{
    printf("\t--------电子辞典--------\n");
    printf("\t-------请选择服务-------\n");
    printf("1.用户注册\n");
    printf("2.用户登录\n");
    printf("0.退出\n");
}
void display_mainui2()
{
    printf("\t-------请选择服务-------\n");
    printf("1.查词\n");
    printf("2.查历史记录\n");
    printf("0.返回\n");
}
static int regist(int sockfd)
{
	char name[20]="";
	char password[20]="";
	struct DictOLPDU *reqpdu = NULL;
	struct DictOLPDU *rsppdu = NULL;
	int ret = 0;
	
	/*1. 接收用户输入的姓名*/
	printf("请输入姓名:\n");
	scanf("%s",name);
	while(getchar()!='\n');
	if(strlen(name)>20)
	{
		printf("输入姓名超过长度限制\n");
		return 1;
	}
	/*2.接收用户输入的密码*/
	printf("请输入密码:\n");
	scanf("%s",password);
	while(getchar()!='\n');
	if(strlen(password)>20)
	{
	    printf("输入密码超过长度限制\n");
	    return 1;
	}
	/*3. 创建注册请求pdu*/
	reqpdu = CreateRegisterREQPDU(name,password,1); 

	/*3. 发送pdu*/
	ret = SendPDU(sockfd,reqpdu);
	/*4. 销毁注册请求pdu*/
	DestroyDictOLPDU(reqpdu);
	reqpdu = NULL;
	if(ret)
	{
		printf("注册请求失败\n");
		return -1;
	}

	/*5. 接收服务端发来的注册回应pdu*/
	rsppdu = RecvPDU(sockfd);
	if(rsppdu == NULL)
	{
		printf("接收注册请求失败\n");
		return -2;
	}
	if(rsppdu->buf == 0)
	{
	    printf("注册成功\n");    
	}
	DestroyDictOLPDU(rsppdu);
	rsppdu = NULL;

	return 0;
}
static int login(int sockfd)
{
	char name[20]="";
	char password[20]="";
	struct DictOLPDU *reqpdu = NULL;
	struct DictOLPDU *rsppdu = NULL;
	int ret = 0;
	
	/*1. 接收用户输入的姓名*/
	printf("请输入姓名:\n");
	scanf("%s",name);
	while(getchar()!='\n');

	if(strlen(name)>20)
	{
		printf("输入姓名超过长度限制\n");
		return 1;
	}
	/*2.接收用户输入的密码*/
	printf("请输入密码:\n");
	scanf("%s",password);
	while(getchar()!='\n');
	if(strlen(password)>20)
	{
	    printf("输入密码超过长度限制\n");
	    return 1;
	}
	/*3. 创建登录请求pdu*/
	reqpdu = CreateLoginREQPDU(name,password); 
	/*3. 发送pdu*/
	ret = SendPDU(sockfd,reqpdu);
	/*4. 销毁登录请求pdu*/
	DestroyDictOLPDU(reqpdu);
	reqpdu = NULL;
	if(ret)
	{
		printf("登录请求失败\n");
		return -1;
	}

	/*5. 接收服务端发来的登录回应pdu*/
	rsppdu = RecvPDU(sockfd);

	if(rsppdu == NULL)
	{
		printf("接收登录请求失败\n");
		return -2;
	}
        if(0 == *(int*)rsppdu->buf)
	{
	    printf("登陆成功!\n");
	}
	else if(1 == *(int*)rsppdu->buf)
	{
	    printf("无该用户!\n");
	    return -3;
	}
	else if(2 == *(int*)rsppdu->buf)
	{
	    printf("该用户已登陆\n");
	    return -4;
	}	
	else if(3 == *(int*)rsppdu->buf)
	{
	    printf("密码错误\n");
	    return -5;
	}	

	DestroyDictOLPDU(rsppdu);
	rsppdu = NULL;
	return 0;
}
static int serach_word(int sockfd)
{
	
	char word[30];
	while(1)
	{
	    bzero(word,30);
	    struct DictOLPDU *reqpdu = NULL;
	    struct DictOLPDU *rsppdu = NULL;
	    int ret = 0;
	
	    /*1. 接收用户输入的单词*/
	    printf("请输入单词(输入0退出):\n");
	    scanf("%s",word);
	    while(getchar()!='\n');
	    if(strlen(word)>30)
	    {
		    printf("输入单词超过长度限制\n");
		    return 1;
	    }
	    if(strcmp(word,"0")==0)
	    {
		    return 0;	
	    }
	    /*2. 创建查词请求pdu*/
	    reqpdu = CreateQueryWordREQPDU(word); 
	    
	    /*3. 发送pdu*/
	    ret = SendPDU(sockfd,reqpdu);
	    /*4. 销毁查词请求pdu*/
	    DestroyDictOLPDU(reqpdu);
	    reqpdu = NULL;
	    if(ret)
	    {
		printf("查词请求失败\n");
		return -1;
	    }
	    /*5. 接收服务端发来的查词回应pdu*/
	    rsppdu = RecvPDU(sockfd);
	    printf("ok\n");
	    if(rsppdu == NULL)
	    {
		printf("接收查词请求失败\n");
		return -2;
	    }
	    printf("%s\n",rsppdu->buf);
	    DestroyDictOLPDU(rsppdu);
	    rsppdu = NULL;
	    }
	    return 0;
}
static int history(int sockfd)
{
	
	//char history[1024];
	struct DictOLPDU *reqpdu = NULL;
	struct DictOLPDU *rsppdu = NULL;
	int ret = 0;
	
	/*1. 创建历史查询请求pdu*/
	reqpdu = CreateHistoryREQPDU();
	    
	/*2. 发送pdu*/
	ret = SendPDU(sockfd,reqpdu);
	/*3. 销毁历史查询请求pdu*/
	DestroyDictOLPDU(reqpdu);
	reqpdu = NULL;
	if(ret)
	{
	    printf("查询历史请求失败\n");
	    return -1;
	}

	/*4. 接收服务端发来的查询历史请求回应pdu*/
	rsppdu = RecvPDU(sockfd);
	if(rsppdu == NULL)
	{
	    printf("接收查询历史请求失败\n");
	    return -2;
	}
	printf("%s\n",rsppdu->buf);
	DestroyDictOLPDU(rsppdu);
	rsppdu = NULL;

	return 0;
}




static int create_client_socket(const char *ip,unsigned short port)
{
	int sockfd = -1;
	struct sockaddr_in servaddr;
	int ret = 0;
	 /*为后面的TCP传输创建一个控制装置(引擎对象或简称为引擎)，系统会给
         * 该引擎提供一个描述符来代表它
         **/
        sockfd = socket(AF_INET,SOCK_STREAM,0);

        /*填写服务端IP地址和端口号*/
        bzero(&servaddr,sizeof(servaddr));//memset(&servaddr,0,sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(port);
        if(!inet_aton(ip,&servaddr.sin_addr))
	{
		printf("IP Address is invalid\n");
                close(sockfd);
                sockfd = -1;
		return -1;
	}

        /*与服务端建立连接，并自动填写客户端自己的IP地址和端口号*/
        ret = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
        if(ret)
        {
                printf("connect server failed\n");
                close(sockfd);
                sockfd = -1;
                return -2;
        }

	return sockfd;
}
